package com.decheng.bootmybatisgenerator.controller;

import com.decheng.bootmybatisgenerator.entities.User;
import com.decheng.bootmybatisgenerator.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Random;

/**
 * @Author 孔德成
 * @Slogan 致敬大师，致敬未来的你
 * @date 2021/8/18
 */
@RestController
@Slf4j
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/user/add")
    public String add() {
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setSex((byte) new Random().nextInt(2));
            user.setUsername("kdc" + i);
            int v = (int) ((Math.random() * 9 + 1) * 100000);
            user.setPassword(v + "");
            userService.add(user);
        }
        return "------add user success";
    }

    @RequestMapping("/user/del/{id}")
    public String del(@PathVariable Integer id) {
        userService.del(id);
        return "------del user success";
    }

    @RequestMapping("/user/update/{id}")
    public int update(@PathVariable Integer id) {
        User user = get(id);
        user.setSex((byte) new Random().nextInt(2));
        user.setUsername("java tk mapper");
        int v = (int) ((Math.random() * 9 + 1) * 100000);
        user.setPassword(v + "");

        return userService.update(user);
    }

    @RequestMapping("/user/{id}")
    private User get(@PathVariable Integer id) {
        return userService.getById(id);
    }

    @RequestMapping("/user/condition")
    public List<User> getByCondition() {
        User user = new User();
        user.setSex((byte) 1);
        user.setDeleted((byte) 0);
        return userService.getByCondition(user);
    }

    @RequestMapping("/user/page")
    public List<User> getSplitPage() {
        User user = new User();
        user.setDeleted((byte) 0);
        return userService.getSplitPage(user);
    }

    @RequestMapping("/user/pageex")
    public List<User> getSplitPageExample() {
        User user = new User();
        user.setUsername("kdc");
        return userService.getSplitPageExample(user);
    }
}
